<?php
namespace Common\Model;

use Common\Util\BaseData;
use Common\Util\Wxrcode;
use Think\Model;

class UserCardListModel extends Model{

	protected $tableName = 'user_card_list';

	/**
	 * 名片详情
	 *
	 * @param int $id
	 * @return array|false|mixed|string|null
	 */
	public function getInfo($where = []){
		if(!$info = $this->where($where)->find()){
			return false;
		}
		return $info;
	}

	public function getCardList($where = [], $order = '', $pagesize = 1, $pagenum = 10, $user_id = 0, $u_lat, $u_lng){
		$startnum = ($pagesize - 1) * $pagenum;
		if(!empty($u_lat) && !empty($u_lng)){
			$sql_filed = "*,(2 * 6378.137* ASIN(SQRT(POW(SIN(3.1415926535898*(".$u_lat."-c_lat)/360),2)+COS(3.1415926535898*".$u_lat."/180)* COS(c_lat * 3.1415926535898/180)*POW(SIN(3.1415926535898*(".$u_lng."-c_lng)/360),2))))*1000 as juli_number";
		}else{
			$sql_filed = "*,0 as juli_number";
		}
		$data_list = $this
			->where($where)
			->field($sql_filed)
			->order($order)
			->limit($startnum, $pagenum)
			->select();
		foreach($data_list as $key => $val){
			if($val['juli_number'] == 0 || empty($val['juli_number'])){
				$data_list[$key]['info_juli_num'] = 0;
				$data_list[$key]['info_juli_mi'] = '0m';
			}else{
				if($val['juli_number'] >= 1000){
					$leng_km = sprintf("%.1f", $val['juli_number'] / 1000);
					$data_list[$key]['info_juli_mi'] = $leng_km.'km';
				}else{
					$data_list[$key]['info_juli_mi'] = sprintf("%.1f", $val['juli_number']).'m';
				}
				$data_list[$key]['info_juli_num'] = round($val['juli_number']);
			}
			$data_list[$key]['zhiye_label'] = $val['zhiye_label'] ? explode(',', $val['zhiye_label']) : [];
			$collect = M("user_card_list_collect")->where(['user_id' => $user_id, 'card_id' => $val['id']])->find();
			$data_list[$key]['collect'] = $collect ? true : false;
		}
		return $data_list ? $data_list : [];
	}

	public function saveViewLog($user_id, $card_id){
		$card_info = $this->getInfo(['id' => $card_id]);
		if($card_info['user_id'] == $user_id){
			return false;
		}
		//		判断之前已经看过多少次主页了
		$view_count = M("user_view_log")->where([
			'user_id'      => $user_id,
			'view_user_id' => $card_info['user_id'],
			'view_type'    => 1,
			'view_card_id' => $card_id,
		])->count();
		$end_view_all_time = M("user_view_log")->where([
			'user_id'      => $user_id,
			'view_user_id' => $card_info['user_id'],
			'view_type'    => 1,
			'view_card_id' => $card_id,
		])->order("id desc")->getField("view_all_time");
		$updateAction = M("user_view_log")->add([
			'user_id'          => $user_id,
			'view_type'        => 1,
			'view_user_id'     => $card_info['user_id'],
			'view_card_id'     => $card_id,
			'view_day'         => date("Y-m-d"),
			'view_count'       => $view_count ? ($view_count + 1) : 1,
			'view_all_time'    => $end_view_all_time ? $end_view_all_time : 0,
            'created_at'    => get_now_date(),
            'updated_at'    => get_now_date(),
		]);
		return $updateAction;
	}

	/**
	 * 生成二维码
	 *
	 * @param int $card_id
	 */
	public function getCardWxrcode($card_id = 0){
		$app_setting = BaseData::getAppSetting();
		$img = Wxrcode::getUserShareWxcode($app_setting['wx_app_appid'], $app_setting['wx_app_appsecret'], 'card_id:'.$card_id, 'pages/index/index');
		return $img ? $img : '';
	}
}
